package com.linlang.grasharepro.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.linlang.grasharepro.R;
import com.linlang.grasharepro.model.MessageCenter;
import com.linlang.grasharepro.model.NewMessageDomain;


import java.util.List;

/**
 * 商家WIFI适配器
 * Created by pengtao on 2016/6/3.
 */
public class MessageAdapter extends BaseAdapter {

    /**
     * 商家信息列表
     */
    private List<NewMessageDomain> list;

    public List<NewMessageDomain> getList() {
        return list;
    }

    public void setList(List<NewMessageDomain> list) {
        this.list = list;
    }

    public Context getCtx() {
        return ctx;
    }

    public void setCtx(Context ctx) {
        this.ctx = ctx;
    }


    private Context ctx;


    public MessageAdapter(List<NewMessageDomain> list,Context ctx) {
        this.ctx = ctx;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Holder hold;
        if (convertView == null) {
            hold = new Holder();
            convertView = View.inflate(ctx, R.layout.item_messagecenter, null);
            hold.mContent = (TextView) convertView.findViewById(R.id.messagecener_content);//内容
            hold.mUser = (TextView) convertView.findViewById(R.id.messagecener_username);//用户名
            hold.mTime = (TextView) convertView.findViewById(R.id.messagecener_time);//发布时间
            hold.mHead = (ImageView) convertView.findViewById(R.id.messagecener_head);
            convertView.setTag(hold);
        } else {
            hold = (Holder) convertView.getTag();
        }

        if(list.size() != 0){
            NewMessageDomain domain = list.get(position);
            hold.mHead.setTag(domain.getImage());
            hold.mUser.setText(domain.getFromUser());
            hold.mTime.setText(domain.getTime());
            hold.mContent.setText(domain.getContent());
        }

        switch (position){
            case 0:
                hold.mHead.setImageResource(R.drawable.shop_photo_frame);
                break;
            case 1:
                hold.mHead.setImageResource(R.drawable.actionbar_icon);
                break;
            case 2:
                hold.mHead.setImageResource(R.drawable.arrow);
                break;
            case 3:
                hold.mHead.setImageResource(R.drawable.atm);
                break;
            case 4:
                hold.mHead.setImageResource(R.drawable.pub_u);
                break;
        }
        // 设置默认显示的图片



        /**
         Bitmap bit = loadImg.loadImage(hold.mHead, ConstantsModel.SHOPLISTIMGURL
         + list.get(position).getHeadUrl(), new LoadImg.ImageDownloadCallBack() {
        @Override public void onImageDownload(ImageView imageView, Bitmap bitmap) {
        // 网络交互时回调进来防止错位
        if (hold.mHead.getTag().equals(
        ConstantsModel.SHOPLISTIMGURL + list.get(position).getHeadUrl())) {
        // 设置网络下载回来图片显示
        hold.mHead.setImageBitmap(bitmap);
        }
        }
        });

         // 从本地获取的
         if (bit != null) {
         // 设置缓存图片显示
         hold.mHead.setImageBitmap(bit);
         }*/

        return convertView;
    }

    static class Holder {
        TextView mUser, mTime, mType, mContent;
        ImageView mHead;
    }
}
